package com.dream.keigezhushou.Activity.acty.personal;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.activity.BaseActivity;
import com.dream.keigezhushou.Activity.adapter.MyExpandableListAdapter;
import com.dream.keigezhushou.Activity.bean.GoodsBean;
import com.dream.keigezhushou.Activity.bean.StoreBean;
import com.dream.keigezhushou.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyBuyCarActivity extends BaseActivity implements View.OnClickListener{

    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.ll_empty_car)
    LinearLayout llEmptyCar;
    @BindView(R.id.id_elv_listview)
    ExpandableListView idElvListview;
    @BindView(R.id.all_check)
    CheckBox allCheck;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.product_num)
    TextView productNum;
    @BindView(R.id.rl_balance)
    RelativeLayout rlBalance;

    List<Map<String, Object>> parentList = new ArrayList<Map<String, Object>>(); //父列的集合
    List<List<Map<String, Object>>> childList = new ArrayList<List<Map<String, Object>>>(); //子列的集合
    @BindView(R.id.rl_foot_view)
    RelativeLayout rlFootView;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.tv_edit_car)
    TextView tvEditCar;

    //自定义父组件数组
//    private String[] storeId = {"乐唱好声音KTV(高新店)","欢乐迪KTV(钟楼店)","方糖KTV(未央店)"};
    private MyExpandableListAdapter adapter;
    private String stringName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_buy_car);
        ButterKnife.bind(this);

        initCarData();
        initView();

    }

    //初始化界面
    protected void initView() {

        tvTitle.setText("购物车");


//        parentList = new ArrayList<String>();
//        parentList.add(String.valueOf(storeId));

        //创建适配器
        adapter = new MyExpandableListAdapter(this, parentList, childList);
        idElvListview.setAdapter(adapter);

        idElvListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        //设置ExpandableListView默认是展开的
        for (int i = 0; i < parentList.size(); i++) {

            idElvListview.expandGroup(i);
        }

        //设置点击父组件不进行收缩
        idElvListview.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });

//        if (parentList.isEmpty() || childList.isEmpty()){
//
//            idElvListview.setVisibility(View.GONE);
//            rlFootView.setVisibility(View.GONE);
//            llEmptyCar.setVisibility(View.VISIBLE);
//        }else {
//
//            idElvListview.setVisibility(View.VISIBLE);
//            rlFootView.setVisibility(View.VISIBLE);
//            llEmptyCar.setVisibility(View.GONE);
//        }


        //点击编辑
        tvEditCar.setOnClickListener(this);
        //全选的点击事件
        allCheck.setOnClickListener(this);
        //结算的点击事件
        rlBalance.setOnClickListener(this);


        adapter.setOnGoodsCheckedChangeListener(new MyExpandableListAdapter.OnGoodsCheckedChangeListener() {
            @Override
            public void onGoodsCheckedChange(int totalCount, double totalPrice) {
                tvPrice.setText(String.valueOf(totalPrice));
                tvPrice.setText(String.valueOf("结算"+" " + "（" + totalCount + ")"));
            }
        });

        adapter.setOnAllCheckedBoxNeedChangeListener(new MyExpandableListAdapter.OnAllCheckedBoxNeedChangeListener() {
            @Override
            public void onCheckedBoxNeedChange(boolean allParentIsChecked) {
                allCheck.setChecked(allParentIsChecked);
            }
        });



        //判断购物车是否有商品
        adapter.setOnCheckHasGoodsListener(new MyExpandableListAdapter.OnCheckHasGoodsListener() {
            @Override
            public void onCheckHasGoods(boolean isHasGoods) {

                setUpViewShow(isHasGoods);
            }
        });

//        initCarData();
//        initData();


    }

    private void setUpViewShow(boolean isHasGoods) {

        if (isHasGoods) {

            idElvListview.setVisibility(View.VISIBLE);
            llEmptyCar.setVisibility(View.GONE);
            rlFootView.setVisibility(View.VISIBLE);
        } else {

            idElvListview.setVisibility(View.GONE);
            llEmptyCar.setVisibility(View.VISIBLE);
            rlFootView.setVisibility(View.GONE);
        }
    }


    //购物车初始化数据
    protected void initCarData() {


        for (int i = 0; i < 3; i++) {
            //提供父列表的数据
            Map<String, Object> parentMap = new HashMap<String, Object>();
            parentMap.put("parentName", "parentName" + i);

            parentMap.put("parentIcon", R.mipmap.shop);

            parentList.add(parentMap);
            //提供当前父列的子列数据
            List<Map<String, Object>> childMapList = new ArrayList<Map<String, Object>>();
            for (int j = 0; j < 4; j++) {
                Map<String, Object> childMap = new HashMap<String, Object>();
                childMap.put("childName", "parentName" + i + "下面的childName" + j);
                childMapList.add(childMap);
            }
            childList.add(childMapList);

        }
    }


    //点击事件的实现

    @Override
    public void onClick(View view) {


        switch (view.getId()){

            case R.id.tv_edit_car:

                if (view instanceof TextView){

                    TextView tvEdit = (TextView) view;
                    if (adapter.EDITING.equals(tvEdit.getText())){

                        adapter.setupEditingAll(true);
                        tvEdit.setText(adapter.FINISH_EDITING);
                    }else {

                        adapter.setupEditingAll(false);
                        tvEdit.setText(adapter.EDITING);
                    }
                }
                break;

            case R.id.all_check:

                if (view instanceof CheckBox){

                    CheckBox cbAll = (CheckBox) view;
                    adapter.setAllCheck(cbAll.isChecked());
                }
                break;
        }
    }


    //初始化数据
//    protected void initData() {
//
//
//        for (int i = 0; i < 3; i++) {
//            //提供父列表的数据
//            Map<String, Object> parentMap = new HashMap<String, Object>();
//            parentMap.put("parentName", "parentName" + i);
//
//            parentMap.put("parentIcon", R.mipmap.shop);
//
//            parentList.add(parentMap);
//            //提供当前父列的子列数据
//            List<Map<String, Object>> childMapList = new ArrayList<Map<String, Object>>();
//            for (int j = 0; j < 4; j++) {
//                Map<String, Object> childMap = new HashMap<String, Object>();
//                childMap.put("childName", "parentName" + i + "下面的childName" + j);
//                childMapList.add(childMap);
//            }
//            childList.add(childMapList);
//
//        }
//
//        ivReturn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                //返回
//                finish();
//            }
//        });
//
//        //全选CheckBox设置点击事件
//        allCheck.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//                if (view instanceof CheckBox) {
//
//                    CheckBox checkBox = (CheckBox) view;
//                    adapter.setAllCheck(checkBox.isChecked());
//                }
//            }
//        });
//    }
}
